home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WBTRIEVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-30  |  9.8 KB  |  397 lines

  1. #include <stdio.h>
  2. #include <WBtrieve.h>
  3. #include <WStr.h>
  4.  
  5. extern "C"
  6.   {
  7.     #include "turcbtrv.c"
  8.   }
  9.  
  10. #pragma hdrstop
  11.  
  12. // copyright (c) 1993 Paul Wheaton
  13.  
  14. struct StatBuffer
  15.   {
  16.     FileSpec F;
  17.     KeySpec K[10];
  18.     char ACS[256];  // alternate collating sequence
  19.   };
  20.  
  21. Btrieve::Btrieve(const char* FileName, int BiggestRecSize,int Mode)
  22.   {
  23.     int BufLen=1;
  24.     Stat=BTRV(0,PosBlock,"",&BufLen,(char*)FileName,Mode);
  25.     if (Stat!=0) FatalError("btopen "+Str(Stat));
  26.     Closed=False;
  27.     BuffyLen=BiggestRecSize;
  28.     Buffy=new char[BuffyLen];
  29.     if (Buffy==NULL) FatalError("btopenb "+Str(BuffyLen));
  30.     LastKey=0;
  31.     ClearArray(Keys);
  32.     StatBuffer SB;
  33.     int SBLen=sizeof(SB);
  34.     char KeyBuffer[64];
  35.     Stat=BTRV(15,PosBlock,(char*)&SB,&SBLen,KeyBuffer,0);
  36.     if (Stat!=0) FatalError("btopens "+Str(Stat));
  37.     int I;
  38.     For(I,SB.F.NumIndexes) Keys[I]=&Buffy[SB.K[I].Pos-1];
  39.   }
  40.  
  41. void Btrieve::Close()
  42.   {
  43.     if (!Closed)
  44.       {
  45.         int Dummy=0;
  46.         BTRV(1,PosBlock,"",&Dummy,"",0);
  47.         delete Buffy;
  48.       }
  49.   }
  50.  
  51. int Btrieve::GetFirst(int KeyNum)
  52.   {
  53.     int BL=BuffyLen;
  54.     Stat=BTRV(12,PosBlock,Buffy,&BL,Keys[KeyNum],KeyNum);
  55.     if (!((Stat==0)||(Stat==22))) BL=0;
  56.     LastKey=KeyNum;
  57.     return BL;
  58.   }
  59.  
  60. int Btrieve::GetLast(int KeyNum)
  61.   {
  62.     int BL=BuffyLen;
  63.     Stat=BTRV(13,PosBlock,Buffy,&BL,Keys[KeyNum],KeyNum);
  64.     if (!((Stat==0)||(Stat==22))) BL=0;
  65.     LastKey=KeyNum;
  66.     return BL;
  67.   }
  68.  
  69. int Btrieve::GetNext()
  70.   {
  71.     int BL=BuffyLen;
  72.     Stat=BTRV(6,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  73.     if (!((Stat==0)||(Stat==22))) BL=0;
  74.     return BL;
  75.   }
  76.  
  77. int Btrieve::GetPrev()
  78.   {
  79.     int BL=BuffyLen;
  80.     Stat=BTRV(7,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  81.     if (!((Stat==0)||(Stat==22))) BL=0;
  82.     return BL;
  83.   }
  84.  
  85. int Btrieve::GetE(int SearchVal, int KeyNum)
  86.   {
  87.     int BL=BuffyLen;
  88.     LastKey=KeyNum;
  89.     *((int*)Keys[LastKey])=SearchVal;
  90.     Stat=BTRV(5,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  91.     if (!((Stat==0)||(Stat==22))) BL=0;
  92.     return BL;
  93.   }
  94.  
  95. int Btrieve::GetE(const char* SearchVal, int KeyNum)
  96.   {
  97.     int BL=BuffyLen;
  98.     LastKey=KeyNum;
  99.     strcpy((char*)Keys[LastKey],SearchVal);
  100.     Stat=BTRV(5,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  101.     if (!((Stat==0)||(Stat==22))) BL=0;
  102.     return BL;
  103.   }
  104.  
  105. int Btrieve::GetE(long SearchVal, int KeyNum)
  106.   {
  107.     int BL=BuffyLen;
  108.     LastKey=KeyNum;
  109.     *((long*)Keys[LastKey])=SearchVal;
  110.     Stat=BTRV(5,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  111.     if (!((Stat==0)||(Stat==22))) BL=0;
  112.     return BL;
  113.   }
  114.  
  115. int Btrieve::GetG(int SearchVal, int KeyNum)
  116.   {
  117.     int BL=BuffyLen;
  118.     LastKey=KeyNum;
  119.     *((int*)Keys[LastKey])=SearchVal;
  120.     Stat=BTRV(8,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  121.     if (!((Stat==0)||(Stat==22))) BL=0;
  122.     return BL;
  123.   }
  124.  
  125. int Btrieve::GetG(const char* SearchVal, int KeyNum)
  126.   {
  127.     int BL=BuffyLen;
  128.     LastKey=KeyNum;
  129.     strcpy((char*)Keys[LastKey],SearchVal);
  130.     Stat=BTRV(8,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  131.     if (!((Stat==0)||(Stat==22))) BL=0;
  132.     return BL;
  133.   }
  134.  
  135. int Btrieve::GetG(long SearchVal, int KeyNum)
  136.   {
  137.     int BL=BuffyLen;
  138.     LastKey=KeyNum;
  139.     *((long*)Keys[LastKey])=SearchVal;
  140.     Stat=BTRV(8,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  141.     if (!((Stat==0)||(Stat==22))) BL=0;
  142.     return BL;
  143.   }
  144.  
  145. int Btrieve::GetGE(int SearchVal, int KeyNum)
  146.   {
  147.     int BL=BuffyLen;
  148.     LastKey=KeyNum;
  149.     *((int*)Keys[LastKey])=SearchVal;
  150.     Stat=BTRV(9,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  151.     if (!((Stat==0)||(Stat==22))) BL=0;
  152.     return BL;
  153.   }
  154.  
  155. int Btrieve::GetGE(const char* SearchVal, int KeyNum)
  156.   {
  157.     int BL=BuffyLen;
  158.     LastKey=KeyNum;
  159.     strcpy((char*)Keys[LastKey],SearchVal);
  160.     Stat=BTRV(9,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  161.     if (!((Stat==0)||(Stat==22))) BL=0;
  162.     return BL;
  163.   }
  164.  
  165. int Btrieve::GetGE(long SearchVal, int KeyNum)
  166.   {
  167.     int BL=BuffyLen;
  168.     LastKey=KeyNum;
  169.     *((long*)Keys[LastKey])=SearchVal;
  170.     Stat=BTRV(9,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  171.     if (!((Stat==0)||(Stat==22))) BL=0;
  172.     return BL;
  173.   }
  174.  
  175. int Btrieve::GetL(int SearchVal, int KeyNum)
  176.   {
  177.     int BL=BuffyLen;
  178.     LastKey=KeyNum;
  179.     *((int*)Keys[LastKey])=SearchVal;
  180.     Stat=BTRV(10,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  181.     if (!((Stat==0)||(Stat==22))) BL=0;
  182.     return BL;
  183.   }
  184.  
  185. int Btrieve::GetL(const char* SearchVal, int KeyNum)
  186.   {
  187.     int BL=BuffyLen;
  188.     LastKey=KeyNum;
  189.     strcpy((char*)Keys[LastKey],SearchVal);
  190.     Stat=BTRV(10,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  191.     if (!((Stat==0)||(Stat==22))) BL=0;
  192.     return BL;
  193.   }
  194.  
  195. int Btrieve::GetL(long SearchVal, int KeyNum)
  196.   {
  197.     int BL=BuffyLen;
  198.     LastKey=KeyNum;
  199.     *((long*)Keys[LastKey])=SearchVal;
  200.     Stat=BTRV(10,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  201.     if (!((Stat==0)||(Stat==22))) BL=0;
  202.     return BL;
  203.   }
  204.  
  205. int Btrieve::GetLE(int SearchVal, int KeyNum)
  206.   {
  207.     int BL=BuffyLen;
  208.     LastKey=KeyNum;
  209.     *((int*)Keys[LastKey])=SearchVal;
  210.     Stat=BTRV(11,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  211.     if (!((Stat==0)||(Stat==22))) BL=0;
  212.     return BL;
  213.   }
  214.  
  215. int Btrieve::GetLE(const char* SearchVal, int KeyNum)
  216.   {
  217.     int BL=BuffyLen;
  218.     LastKey=KeyNum;
  219.     strcpy((char*)Keys[LastKey],SearchVal);
  220.     Stat=BTRV(11,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  221.     if (!((Stat==0)||(Stat==22))) BL=0;
  222.     return BL;
  223.   }
  224.  
  225. int Btrieve::GetLE(long SearchVal, int KeyNum)
  226.   {
  227.     int BL=BuffyLen;
  228.     LastKey=KeyNum;
  229.     *((long*)Keys[LastKey])=SearchVal;
  230.     Stat=BTRV(11,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  231.     if (!((Stat==0)||(Stat==22))) BL=0;
  232.     return BL;
  233.   }
  234.  
  235. Bool Btrieve::Update()
  236.   {
  237.     int BL=BuffyLen;
  238.     Stat=BTRV(3,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  239.     return (Stat==0);
  240.   }
  241.  
  242. Bool Btrieve::Update(int RecLen)
  243.   {
  244.     int BL=RecLen;
  245.     Stat=BTRV(3,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  246.     return (Stat==0);
  247.   }
  248.  
  249. Bool Btrieve::Insert(const void* Rec)
  250.   {
  251.     int BL=BuffyLen;
  252.     Stat=BTRV(2,PosBlock,(Rec==NULL?Buffy:(char*)Rec),&BL,Keys[LastKey],LastKey);
  253.     return (Stat==0);
  254.   }
  255.  
  256. Bool Btrieve::Insert(const void* Rec,int Len)
  257.   {
  258.     int BL=Len;
  259.     Stat=BTRV(2,PosBlock,(Rec==NULL?Buffy:(char*)Rec),&BL,Keys[LastKey],LastKey);
  260.     return (Stat==0);
  261.   }
  262.  
  263. void Btrieve::DelCur() // the last record loaded up is killed from the database
  264.   {
  265.     Stat=BTRV(4,PosBlock,Buffy,&BuffyLen,Keys[LastKey],LastKey);
  266.   }
  267.  
  268. long Btrieve::Position()
  269.   {
  270.     long P;
  271.     int PL=4;
  272.     Stat=BTRV(22,PosBlock,(char*)&P,&PL,(char*)&P,0);
  273.     return P;
  274.   }
  275.  
  276. int Btrieve::GetDirect(long Pos)
  277.   {
  278.     *((long*)Buffy)=Pos;
  279.     int BL=BuffyLen;
  280.     Stat=BTRV(23,PosBlock,Buffy,&BL,Keys[LastKey],LastKey);
  281.     if (!((Stat==0)||(Stat==22))) BL=0;
  282.     return BL;
  283.   }
  284.  
  285. static struct AlternateCollatingSequence
  286.   {
  287.     char Signature;
  288.     char Name[8];
  289.     char Seq[256];
  290.   } ACS;
  291.  
  292. CreateBtrieveFile::CreateBtrieveFile(const char* FileName, int RecSize,
  293.     int PageSize)
  294.   {
  295.     Name=FileName;
  296.     F.RecLen=RecSize;
  297.     F.PageSize=PageSize;
  298.     F.PreAllocation=0;
  299.     Closed=False;
  300.     IncludeACS=False;
  301.   }
  302.  
  303. void CreateBtrieveFile::Done()
  304.   {
  305.     if (!Closed)
  306.       {
  307.         Closed=True;
  308.         F.Unused=0;
  309.         F.Reserved=0;
  310.         F.NumIndexes=int(K.Size());
  311.         /*  the following chunk of code was used for debugging
  312.         puts("\n\nCreating "+String(Name));
  313.         puts("\n  rec len = "+Str(F.RecLen));
  314.         puts("page size = "+Str(F.PageSize));
  315.         puts("  indexes = "+Str(F.NumIndexes));
  316.         String S="    flags = ";
  317.         int I;
  318.         For(I,16) S+=Str(F.Flags[I])+' ';
  319.         puts(S);
  320.         puts(" prealloc = "+Str(F.PreAllocation));
  321.         For(I,F.NumIndexes)
  322.           {
  323.             Delay(1500);
  324.             puts("\nIndex "+Str(I));
  325.             puts("          pos = "+Str(K[I].Pos));
  326.             puts("          len = "+Str(K[I].Len));
  327.             String S="        flags = ";
  328.             int J;
  329.             For(J,16) S+=Str(K[I].Flags[J])+' ';
  330.             puts(S);
  331.             puts("extended type = "+Str(K[I].ExtendedType));
  332.             puts("     null val = "+Str(K[I].NullValue));
  333.           }
  334.         */
  335.         int BufSize1=16*(F.NumIndexes+1);
  336.         int BufSize=BufSize1;
  337.         if (IncludeACS) BufSize+=sizeof(ACS);
  338.         char* Buf=new char[BufSize];
  339.         memcpy(Buf,&F,16);
  340.         memcpy(&Buf[16],&K[0],F.NumIndexes*16);
  341.         if (IncludeACS) memcpy(&Buf[BufSize1],&ACS,sizeof(ACS));
  342.         char PosBlock[128];
  343.         int Status=BTRV(14,PosBlock,Buf,&BufSize,(char*)Name,0);
  344.         if (Status!=0) FatalError("BT create "+Str(Status));
  345.         delete(Buf);
  346.       }
  347.   }
  348.  
  349. void CreateBtrieveFile::SetFreeSpaceThreshold(int Percent)
  350.   {
  351.     if ((Percent==10)||(Percent==20)||(Percent==30))
  352.       {
  353.         if (Percent>=20)
  354.           {
  355.             F.Flags[7]=On;
  356.             Percent-=20;
  357.           }
  358.         if (Percent==10) F.Flags[6]=On;
  359.       }
  360.   }
  361.  
  362. void CreateBtrieveFile::PreAllocatePages(int Pages)
  363.   {
  364.     if (Pages>0)
  365.       {
  366.         F.PreAllocation=Pages;
  367.         F.Flags[2]=On;
  368.       }
  369.   }
  370.  
  371. void CreateBtrieveFile::DefineKey(int Pos, int Len, BtrieveKeyType BKT,
  372.     Bool Duplicates, Bool Modifiable, int KeyNum)
  373.   {
  374.     KeySpec KS;
  375.     KS.Pos=Pos+1;   // btrieve calls "1" the first byte
  376.     KS.Len=Len;
  377.     KS.NotUsed=0;
  378.     KS.NullValue=0;
  379.     KS.Reserved=0;
  380.     KS.Flags[0]=Duplicates;
  381.     KS.Flags[1]=Modifiable;
  382.     KS.Flags[8]=True; // type is extended
  383.     KS.ExtendedType=char((BKT==BKInt)?1:11);
  384.     if (BKT==BKString)
  385.       {
  386.         ACS.Signature='\xac';
  387.         strcpy(ACS.Name,"lower  ");
  388.         int I;
  389.         For(I,256) ACS.Seq[I]=I;
  390.         for(I='A';I<='Z';I++) ACS.Seq[I]=I+32;
  391.         KS.Flags[5]=True;
  392.         IncludeACS=True;
  393.       }
  394.     K[KeyNum]=KS;
  395.   }
  396.  
  397.